﻿Public Class update

    Public dbConnSQL As New OleDb.OleDbConnection
    Public dbSrc As String

    '## 업데이트
    Private Sub updateInventory()

        ProgressBar.Value = 5
        Dim wc As New System.Net.WebClient()
        Dim sql As String = ""
        Dim fileLink As String = ""

        addLog("업데이트를 시작합니다......", 1000)
        addLog("업데이트 서버에 접속 ...... [OK]", 1000)
        sql = "SELECT TOP 1 fileLink FROM InventoryManInfo WHERE flag='Y' ORDER BY addDt DESC"
        fileLink = sqlAFieldValue(sql)
        addLog("업데이트 파일 검색 ...... [OK]", 1000)

        wc.DownloadFile(fileLink, "inventory_new.exe")

        addLog("업데이트 파일 다운로드 ...... [OK]", 1000)
        Try
            My.Computer.FileSystem.RenameFile(Application.StartupPath + "\inventoryMan.exe", "inventoryMan_old.exe")

            txtResult.Text += "이전 버전 파일 백업 ...... [OK]" + vbCrLf
            Threading.Thread.Sleep(1000)
        Catch ex As Exception
            'MsgBox(ex.Message)
            addLog("이전 버전 파일이 이미 존재하여 삭제후 이전 버전 파일 백업 ... [OK]", 1000)
            If My.Computer.FileSystem.FileExists(Application.StartupPath + "\inventoryMan_old.exe") = True Then
                My.Computer.FileSystem.DeleteFile(Application.StartupPath + "\inventoryMan_old.exe")
                My.Computer.FileSystem.RenameFile(Application.StartupPath + "\inventoryMan.exe", "inventoryMan_old.exe")
            End If
        End Try
        Try
            ProgressBar.Value = 90
            My.Computer.FileSystem.RenameFile(Application.StartupPath + "\inventory_new.exe", "inventoryMan.exe")
            txtResult.Text += "업데이트 완료 ...... [OK]"

            ProgressBar.Value = 100
            MsgBox("업데이트가 완료 되었습니다")
            ProgressBar.Value = 0
        Catch ex As Exception
        End Try
        Me.Dispose()
    End Sub

    Private Sub addLog(ByVal msg As String, ByVal interval As Integer)
        txtResult.Text += msg + vbCrLf
        txtResult.Refresh()
        Threading.Thread.Sleep(interval)
        ProgressBar.Value += 15
    End Sub

    '## 쿼리에 대한 하나의 값 반환하기
    Private Function sqlAFieldValue(ByVal query As String) As String

        Call dbSqlConnection()
        Dim dsSqlCommon As DataSet
        Dim daSqlCommon As OleDb.OleDbDataAdapter
        Dim rtnValue As String
        dsSqlCommon = New DataSet
        daSqlCommon = New OleDb.OleDbDataAdapter(query, dbConnSQL)
        daSqlCommon.Fill(dsSqlCommon, "aFieldValue")

        dbConnSQL.Close()
        rtnValue = dsSqlCommon.Tables("aFieldValue").Rows(0).Item(0)

        Return rtnValue
    End Function

    Private Sub dbSqlConnection()
        dbSrc = "provider=sqloledb;server=inventory.jadesoft.co.kr;database=noahtech;uid=noahtech;pwd=shdkxpr7;"
        'MsgBox(dbSrc)
        dbConnSQL.ConnectionString = dbSrc
        dbConnSQL.Open()
    End Sub


    Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs)
        Call updateInventory()
    End Sub

    Private Sub update_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed
        Shell(Application.StartupPath + "\inventoryMan.exe", AppWinStyle.NormalNoFocus)
    End Sub


    Private Sub update_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown

        Dim sql, updateMsg, msg, latestVersion As String
        sql = "SELECT TOP 1 updateMsg FROM InventoryManInfo WHERE flag='Y' ORDER BY addDt DESC"
        msg = sqlAFieldValue(sql)
        sql = "SELECT TOP 1 latestVersion FROM InventoryManInfo WHERE flag='Y' ORDER BY addDt DESC"
        latestVersion = sqlAFieldValue(sql)
        lbVersion.Text = "Ver " + latestVersion
        lbVersion.Refresh()
        Label1.Refresh()
        Label2.Refresh()
        gBxUpdateProgress.Text = " [업데이트 진행] "
        gBxUpdateProgress.Refresh()
        StatusStrip1.Refresh()

        updateMsg = vbCrLf + msg + vbCrLf
        'updateMsg += " - 끝 - "
        txtUpdateMsg.Text = updateMsg
        txtUpdateMsg.Refresh()
        System.Threading.Thread.Sleep(1000)
        Call updateInventory()
    End Sub

End Class
